<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2017/10/18
 * Time: 14:33
 */
namespace App\Http\Admin\StockIn\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Eloquent\Admin\StockIn as EloquentStockIn;

class StockIn extends Model
{
    public function getProList()
    {
        return EloquentStockIn::all();
    }

    public function addStockIn($saveData)
    {
        $StockInInfo = EloquentStockIn::firstOrNew(['product_no' => $saveData['product_no']]);
        if ($StockInInfo->id) xThrow(ERR_CLIENT_USER_ALREADY_EXISTS);

        return $this->saveStockIn($StockInInfo, $saveData);
    }

    public function updateStockIn($StockInId, $saveData)
    {
        $StockInInfo = EloquentStockIn::find($StockInId);
        if (!$StockInInfo) xThrow(ERR_CLIENT_USER_NOT_EXIST);

        return $this->saveStockIn($StockInInfo, $saveData);
    }

    private function saveStockIn(EloquentStockIn $StockInInfo, array $saveData)
    {
        $StockInInfo->fill($saveData);
        return $StockInInfo->save();
    }
}